<?php
namespace app\web\controller;

use app\web\library\WebController;
use app\admin\library\User;

class Article extends WebController{
    public function onRead($preview = false){
        $article_id = $this->get('id',$this->check()->type('int')->required());

        $this->pageCache('article_'.$article_id);

        $article =  $this->model('Article');
        $articleData = $article->getId($article_id);

        if (!$articleData){
            $this->page404();
        }

        
        //获取栏目信息
        $part =  $this->model('Part');
        $partData = $part->frontData($articleData['part_id']);
        $this->set('part',$partData);
        $this->set('article',$article->frontData($articleData,$partData));
        $template  = '';
        if ($articleData['template']!=''){
            $template =  $articleData['template'];
        }else{
            if ($partData['row']['article_template']!=''){
                $template =  $partData['row']['artcile_template'];
            }else{
                $config = $this->app->config('template',true);
                $template = $config->article_template;
            }
        }

        return $this->display($template);
    }


    public function onPreview(){
        if (!User::isLogin()) throw new \Exception('请先登录');

        $this->onRead(true);
    }
}